<?php

namespace Xrfg\Api\Trading\Account\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Xrfg\Core\Trading\Account\Models\Coin;
use Xrfg\Core\Trading\Account\Models\Recharge;
use Xrfg\Api\Trading\Account\Resources\RechargeResource;

class RechargeController extends Controller
{
    /**
     * 充值记录.
     *
     * @param Coin    $coin
     * @param Request $request
     *
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function index(Coin $coin, Request $request)
    {
        $perPage = $request->query('per_page', 15);
        $perPage = $perPage > 100 ? 100 : $perPage;

        $recharges = Recharge::where('user_id', Auth::id())
            ->when($coin->exists, function (Builder $query) use ($coin) {
                return $query->where('coin_id', $coin->id);
            })
            ->orderByDesc('id')
            ->paginate($perPage);

        return RechargeResource::collection($recharges);
    }
}
